<!-- 充值详情统计 -->
<template>
  <div>
    <el-card>
      <div class="topInquire">
        <div>
          <!-- 渠道 -->
          <Channel @channel-event="handleSelectChannel"></Channel>
        </div>

        <div>
        <!-- 服务器 -->
        <Server @server-event="handleSelectServer"></Server>
        </div>

        <div>
        <!-- 日期 -->
        <DatePicker @dateChange="handleDateChange"></DatePicker>
        </div>

        <el-button 
        size="small" 
        icon="el-icon-search" 
        type="primary" @click="handleSearch">搜索</el-button>

        <el-button icon="el-icon-upload2" type="success" 
          size="small" @click="handleExport">导出Excel</el-button>

      </div>

      <div>
        <el-table
          ref="table"
          border
          :data="rechargeSumData"
          :height="tableHeight"
          show-summary
        >
          <el-table-column prop="productId" label="商品ID" min-width="120" />
          <el-table-column prop="productName" label="商品名称" min-width="120" />
          <el-table-column prop="productType" label="商品类型" min-width="120" />
          <el-table-column prop="price" label="商品价格" min-width="120" />
          <el-table-column prop="totalPay" label="付费总额" min-width="120" />
          <el-table-column prop="buyNum" label="购买次数" min-width="120" />
          <el-table-column prop="numRate" label="次数占比" min-width="120" />
          <el-table-column prop="payRate" label="充值占比" min-width="120" />
    
        </el-table>

        <!-- 分页 -->
        <el-pagination
        background
        layout="total, prev, pager, next, jumper"
        :total="total"
        :current-page="currentPage"
        :page-size="pageSize"
        @current-change="changePage"
        />
      </div>
    </el-card>

  </div>
</template>

<script>
import Channel from '@/components/selectChannel/index.vue'
import Server from '@/components/selectServer/index.vue'
import DatePicker from '@/components/datePicker/index.vue'
import { export_json_to_excel } from '@/widget/Export2Excel'

import { rechargeSum  } from '@/api/relatedRecharge'

export default {
  components: {
   Channel,Server,DatePicker
  },
  data() {
    return {
      rechargeSumData:[],
      currentPage: 1, // 当前页
      pageSize: 200, // 每页显示条数
      tableHeight: 0, // 表格高度
      selectChannel:[],
      selectServer:[],
      date:[],
      total:0
    }
  },

  created() {
    this.getRechargeSum()
  },

  mounted() {
    this.$nextTick(() => {
      this.getTableHeight()
    })
    window.addEventListener('resize', this.$debounce(this.getTableHeight, 100))
  },

  beforeDestroy() {
    window.removeEventListener('resize', this.getTableHeight)
  },

  methods: {
    // 获取数据
    async getRechargeSum() {
      const res = await rechargeSum({
        page: this.currentPage,
        limit: this.pageSize
      })
      this.total = res.data.data.count
      this.rechargeSumData = res.data.data.data
    },

    // 选择日期
    handleDateChange(dateRange){
      this.date = dateRange
    },

    // 选中的渠道
    handleSelectChannel(value){
      this.selectChannel = value
    },

    // 选中的服务器
    handleSelectServer(value){
      this.selectServer = value
    },


    // 搜索
    async handleSearch() {
      try {
        const query = {}
        if (this.date && this.date.length === 2) {
          // 时间戳毫秒转化为秒
          query.startTime = this.$moment(this.date[0]).unix()
          query.endTime = this.$moment(this.date[1]).unix()
        }
        if (this.selectChannel.length !== 0) {
          query.channels = this.selectChannel
        }
        if (this.selectServer.length !== 0) {
          query.serverUids = this.selectServer
        }
        const result = await rechargeSum(query)
        this.rechargeSumData = result.data.data.data
      } catch (error) {
        console.error(error)
        this.$message.error('发生错误，请稍后重试')
      }
    },

    // 切换页面
    changePage(page) {
      this.currentPage = page
      this.getRechargeSum()
    },

    // 表格自适应高度
    getTableHeight() {
      if (this.$refs.table && this.$refs.table.$el) {
        this.tableHeight = window.innerHeight - this.$refs.table.$el.offsetTop - 170
      } else {
        this.tableHeight = 300
      }
    },

    // 导出
     async handleExport() {
      try {
        const res = await rechargeSum({
          limit: this.total
        })

        const newData = res.data.data.data.map(item => {
          const arr = []
          const obj = {
            productId: '商品ID',
            productName: '商品名称',
            productType: '商品类型',
            price: '商品价格',
            totalPay:'付费总额',
            buyNum: '购买次数',
            numRate: '次数占比',
            payRate: '充值占比'
          }
          for (const key in obj) {
            arr.push(item[key])
          }
          return arr
        })

        const tHeader = [
          '商品ID', '商品名称', '商品类型', '商品价格','付费总额','购买次数', '次数占比', '充值占比']

        export_json_to_excel({
          header: tHeader,
          data: newData,
          filename: `充值详情统计表${this.$moment(new Date()).format('YYYYMMDDHHmm')}`,
          autoWidth: true,
          bookType: 'xlsx'
        })
      } catch (error) {
        this.$message.error(error)
      }
    },
  }
}
</script>
<style lang="scss" scoped>

.topInquire{
  font-weight: bold;
  font-size: 14px;
  color: #6a7488;
  margin-bottom: 20px;
  display: flex;
  flex-wrap: wrap;
  gap: 30px;
}

</style>
